﻿#include "configmanager.h"
#include <iostream>

#include <QFile>
#include <QTextStream>

using namespace std;
ConfigManager::ConfigManager() {
}

bool ConfigManager::loadConfigFile(QString configFilePath) {
    m_configFilePath = configFilePath;

    // 创建文件对象
    QFile file(m_configFilePath);
    if (!file.exists()) {
        return false;
    }

    // 打开文件，只读方式
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        // 文件不存在，创建一个空的配置文件
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            // 创建文件失败
            return false;
        }
        // 关闭文件
        file.close();
        // 重新打开文件以读取
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            // 打开文件失败
            return false;
        }
    }

    // 创建文本流对象
    QTextStream in(&file);

    // 读取文件内容
    QString content = in.readAll();
    return loadConfig(content);
}

bool ConfigManager::loadConfig(QString configContent) {
    if (configContent.trimmed().isEmpty()) {
        return false;
    }
    std::istringstream iss(configContent.toStdString());
    try {
        m_configRootNode = toml::parse<toml::preserve_comments>(iss);
        return true;
    } catch (const toml::exception &err) {
        std::cout << "parse toml error: " << err.what() << std::endl;
        return false;
    } catch (...) {
        std::cout << "parse toml error" << std::endl;
        return false;
    }
}

bool ConfigManager::saveConfig() {
    if (m_configFilePath.isNull() || m_configFilePath.isEmpty()) {
        return false;
    }

    // 使用TOML11的格式化函数，并指定格式选项
    std::string toml_string = toml::format(m_configRootNode, 2);

    QFile file(m_configFilePath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        return false;
    }
    QTextStream out(&file);
    out << QString::fromStdString(toml_string);

    file.close();
    return true;
}

QString ConfigManager::getConfigFilePath() const {
    return m_configFilePath;
}

QString ConfigManager::getConfig(QString group, QString name, QString defaultValue) {
    std::string value = toml::find_or(m_configRootNode,
                                      group.toStdString(),
                                      name.toStdString(),
                                      defaultValue.toStdString());
    return QString::fromStdString(value);
}

QString ConfigManager::getConfig(QString name, QString defaultValue) {
    std::string value = toml::find_or(m_configRootNode,
                                      name.toStdString(),
                                      defaultValue.toStdString());
    return QString::fromStdString(value);
}

bool ConfigManager::setConfig(QString group, QString name, QString value) {
    m_configRootNode[group.toStdString()][name.toStdString()] = value.toStdString();
    return true;
}

bool ConfigManager::setConfig(QString name, QString value) {
    m_configRootNode[name.toStdString()] = value.toStdString();
    return true;
}

bool ConfigManager::getBoolConfig(QString group, QString name, bool defaultValue) {
    bool value = toml::find_or(m_configRootNode, group.toStdString(), name.toStdString(), defaultValue);
    return value;
}

bool ConfigManager::getBoolConfig(QString name, bool defaultValue) {
    bool value = toml::find_or(m_configRootNode, name.toStdString(), defaultValue);
    return value;
}

bool ConfigManager::setBoolConfig(QString group, QString name, bool value) {
    m_configRootNode[group.toStdString()][name.toStdString()] = value;
    return true;
}

bool ConfigManager::setBoolConfig(QString name, bool value) {
    m_configRootNode[name.toStdString()] = value;
    return true;
}

long ConfigManager::getLongConfig(QString group, QString name, long defaultValue) {
    long value = toml::find_or(m_configRootNode,
                               group.toStdString(), name.toStdString(),
                               defaultValue);
    return value;
}

bool ConfigManager::setLongConfig(QString group, QString name, long value) {
    m_configRootNode[group.toStdString()][name.toStdString()] = value;
    return true;
}

long ConfigManager::getLongConfig(QString name, long defaultValue) {
    long value = toml::find_or(m_configRootNode,
                               name.toStdString(),
                               defaultValue);
    return value;
}

bool ConfigManager::setLongConfig(QString name, long value) {
    m_configRootNode[name.toStdString()] = value;
    return true;
}

int ConfigManager::getIntConfig(QString group, QString name, int defaultValue) {
    long value = toml::find_or(m_configRootNode,
                               group.toStdString(), name.toStdString(),
                               defaultValue);
    return value;
}

bool ConfigManager::setIntConfig(QString group, QString name, int value) {
    m_configRootNode[group.toStdString()][name.toStdString()] = value;
    return true;
}

int ConfigManager::getIntConfig(QString name, int defaultValue) {
    long value = toml::find_or(m_configRootNode,
                               name.toStdString(),
                               defaultValue);
    return value;
}

bool ConfigManager::setIntConfig(QString name, int value) {
    m_configRootNode[name.toStdString()] = value;
    return true;
}

QString ConfigManager::getParams(QString paramName, QString defaultValue) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return getConfig(paramName, defaultValue);
        break;
    case 2:
        return getConfig(params[0], params[1], defaultValue);
        break;
    }
    return defaultValue;
}

bool ConfigManager::getBoolParams(QString paramName, bool defaultValue) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return getBoolConfig(paramName, defaultValue);
        break;
    case 2:
        return getBoolConfig(params[0], params[1], defaultValue);
        break;
    }
    return defaultValue;
}

bool ConfigManager::setParams(QString paramName, QString value) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return setConfig(paramName, value);
        break;
    case 2:
        return setConfig(params[0], params[1], value);
        break;
    }
    return false;
}

bool ConfigManager::setBoolParams(QString paramName, bool value) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return setBoolConfig(paramName, value);
        break;
    case 2:
        return setBoolConfig(params[0], params[1], value);
        break;
    }
    return false;
}

long ConfigManager::getLongParams(QString paramName, long defaultValue) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return getLongConfig(paramName, defaultValue);
        break;
    case 2:
        return getLongConfig(params[0], params[1], defaultValue);
        break;
    }
    return defaultValue;
}

bool ConfigManager::setLongParams(QString paramName, long value) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return setLongConfig(paramName, value);
        break;
    case 2:
        return setLongConfig(params[0], params[1], value);
        break;
    }
    return false;
}

int ConfigManager::getIntParams(QString paramName, int defaultValue) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return getIntConfig(paramName, defaultValue);
        break;
    case 2:
        return getIntConfig(params[0], params[1], defaultValue);
        break;
    }
    return defaultValue;
}

bool ConfigManager::setIntParams(QString paramName, int value) {
    QStringList params = paramName.split(".");
    switch (params.size()) {
    case 1:
        return setIntConfig(paramName, value);
        break;
    case 2:
        return setIntConfig(params[0], params[1], value);
        break;
    }
    return false;
}

void ConfigManager::setConfigFilePath(const QString &configFilePath) {
    m_configFilePath = configFilePath;
}
